package com.neugh;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class GeneratorServiceEntity {
	public static void main(String[] args) {
			new GeneratorServiceEntity().generateCode();
		}
	   
	    public void generateCode() {
	        String packageName = "com.neugh";
	        boolean serviceNameStartWithI = false;//user -> UserService, 设置成true: user -> IUserService
			this.generateByTables(serviceNameStartWithI, packageName,"repair");
	    }
	
	    private void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
	      
		  String dbUrl = "jdbc:mysql://localhost:3306/ticy?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=GMT%2B8";
	      DataSourceConfig dataSourceConfig = new DataSourceConfig();
	      dataSourceConfig.setDbType(DbType.MYSQL)
	              .setUrl(dbUrl)
	              .setUsername("root")
	              .setPassword("1234")
					.setDriverName("com.mysql.cj.jdbc.Driver");
	      
	    	GlobalConfig config = new GlobalConfig();
	        config.setActiveRecord(false)
	                .setAuthor("sot")
	                .setOutputDir("D:\\ider\\cangku\\neughlinlibang\\Neugh\\itcy\\src\\main\\java")
	                .setFileOverride(true)
	                .setBaseResultMap(true)//baseResultMap
	        		.setBaseColumnList(true);
	
	        StrategyConfig strategyConfig = new StrategyConfig();
	        strategyConfig
	                .setCapitalMode(true)
	                .setEntityLombokModel(true)//实体类Lombok
	                .setRestControllerStyle(false)//是否rest风格
	                .setEntityColumnConstant(true)//生成列的常量
	//                .setControllerMappingHyphenStyle(true)//mapping下划线风格
	//                .setTablePrefix("smbms_")//去除表名前缀
		            .setNaming(NamingStrategy.no_change)//下划线转驼峰
	                .setInclude(tableNames);//修改替换成你需要的表名，多个表名传数组
	        		
	        if (!serviceNameStartWithI) {
	            config.setServiceName("%sService");
	        }
	
	        PackageConfig pkConfig = new PackageConfig();
	        pkConfig.setParent(packageName)
	                .setController("controller")
	                .setEntity("entity")
	        		.setMapper("mapper")//dao
	        		.setService("service")//service
	        		.setXml("mapper");//xml
	        
	        new AutoGenerator().setGlobalConfig(config)
	                .setDataSource(dataSourceConfig)
	                .setStrategy(strategyConfig)
	                .setPackageInfo(pkConfig)
	                .execute();
	    }
	
	    private void generateByTables(String packageName, String... tableNames) {
	        generateByTables(true, packageName, tableNames);
	    }
}
